﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using UniversityCourses.Models;
using UniversityCourses.Filters;

namespace UniversityCourses.Controllers
{
    [SessionExpireFilter(Controller = "Account", Action = "Login")]
    public class ManageController : Controller
    {
        IUniversityCoursesRepository repository;

        public ManageController() : this(new UniversityCoursesRepository()) { }
        public ManageController(IUniversityCoursesRepository repository)
        {
            this.repository = repository;
        }

        //default method
        [HttpGet]
        public ViewResult Courses()
        {
            return View(repository.GetAllCourses());
        }

        //passing the data with json
        //returns an array of registered courses
        //we could pass this data on inital load from the server instead, no ajax is needed here in a normal app
        [HttpGet]
        public JsonResult GetRegisteredCoursesAsJson()
        {
            return Json(repository.GetCoursesByStudentEmail(User.Identity.Name), JsonRequestBehavior.AllowGet);
        }
        
        //update
        public ContentResult UpdateCourse(Course course)
        {
            repository.UpdateCourse(course);
            return Content("updated course " + course.Name);
        }

        //delete
        public ContentResult DeleteCourse(int id)
        {
            repository.DeleteCourse(id);
            return Content("deleted " + id);
        }

        //insert
        [HttpGet]
        public PartialViewResult _CourseBox(Course course)
        {
            repository.CreateCourse(course);
            return PartialView(course);
        }

        //register
        public ContentResult Register(int id)
        {
            repository.Register(id, User.Identity.Name);
            return Content("registered " + id);
        }

        //edit dialog
        [HttpGet]
        public ViewResult EditModal(int id)
        {
            return View(repository.GetCoursesById(id));
        }

        //insert dialog
        [HttpGet]
        public ViewResult CreateModal()
        {
            return View();
        }
    }
}
